package com.xl98.controller;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.xl98.entity.Comment;
import com.xl98.entity.ResponseCode;
import com.xl98.entity.Result;
import com.xl98.entity.ResultFactory;
import com.xl98.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-22
 */
@RestController
@Api(value = "评论",tags = {"评论"})
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
     private CommentService commentService;
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加评论")
    public Result addComment (@RequestBody Map<String, String> map) throws ParseException {
        Comment comment = new Comment();
        comment.setArticleId(map.get("articleId"));
        comment.setFromUserId(map.get("userId"));
        comment.setMsgContent(map.get("msgContent"));
        int rs = commentService.addComment(comment);
        if(rs>0){
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_INSERT,null);
        }
        else{
            return ResultFactory.buildFailResult(ResponseCode.FAIL_INSERT);
        }
    }


    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ApiOperation(value = "查询文章下的评论")
    public Result findCommentByarticleId (@RequestBody Map<String, String> map) throws ParseException {
        List<Comment> all = commentService.findCommentByarticleId(map);
        Map<String, Comment> commentMap = new HashMap<>();
        List<Comment> result = new ArrayList<>();
        for (Comment c : all) {
            if (c.getParentId() == null) {
                result.add(c);
            }
            commentMap.put(c.getId(), c);
        }
        for (Comment c : all) {
            if (c.getParentId() != null) {
                Comment parent = commentMap.get(c.getParentId());
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(c);
            }
        }
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,result);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @ApiOperation(value = "点赞或踩")
    public Result updateCount (@RequestBody Map<String, String> map) {
        int rs = commentService.updateCount(map);
        if(rs>0){
            return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_UPDATE);
        }
        else return ResultFactory.buildFailResult(ResponseCode.FAIL_UPDATE);
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除评论")
    public Result delete(String id) {
        return ResultFactory.buildSuccessResult(commentService.delete(id));
    }
}

